// NotesFieldDlg.cpp : implementation file
//

#include "stdafx.h"
#include "NotesField.h"
#include "NotesFieldDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "cNotesField.h"
#include ".\notesfielddlg.h"

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA


	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNotesFieldDlg dialog

CNotesFieldDlg::CNotesFieldDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CNotesFieldDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CNotesFieldDlg)
	m_strNotesFieldName = _T("");
	m_strSharePointFieldName = _T("");
	m_strSharePointFieldType = _T("");
	m_strFileName = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	n_Pos=0;

}

void CNotesFieldDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CNotesFieldDlg)
	DDX_Control(pDX, IDC_SLIDER1, m_slider);
	DDX_Text(pDX, IDC_EDIT1, m_strNotesFieldName);
	DDX_Text(pDX, IDC_EDIT2, m_strSharePointFieldName);
	DDX_CBString(pDX, IDC_COMBO1, m_strSharePointFieldType);
	DDX_Text(pDX, IDC_EDIT3, m_strFileName);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CNotesFieldDlg, CDialog)
	//{{AFX_MSG_MAP(CNotesFieldDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ADD, OnAdd)
	ON_BN_CLICKED(IDC_OPEN, OnOpen)
	ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER1, OnReleasedcaptureSlider1)
	ON_BN_CLICKED(IDC_REMOVE, OnRemove)
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_UPDATE, OnBnClickedUpdate)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CNotesFieldDlg message handlers

BOOL CNotesFieldDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	m_slider.SetRangeMin(0);

	m_slider.SetRangeMax(0);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CNotesFieldDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CNotesFieldDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CNotesFieldDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CNotesFieldDlg::OnOK() 
{
	UpdateData(TRUE);
	CFile theFile;
	TRY
		{
			if(theFile.Open(m_strFileName, CFile::modeCreate | CFile::modeWrite))
				{
					CArchive archive(&theFile, CArchive::store);


					m_objArrayCustomFields.Serialize(archive);
					archive.Flush();
					archive.Close();
					theFile.Close();
				}
			else
				{
					CString cInfo;
					cInfo.Format("Could not open the file %s",m_strFileName);
					AfxMessageBox(cInfo);
					return;
				}

		}
	CATCH( CFileException, e )
		{
			if( e->m_cause == CFileException::fileNotFound )
        MessageBox( "ERROR: File not found\n");
			else
				e->ReportError();
		}
	END_CATCH
	
	CDialog::OnOK();
}

void CNotesFieldDlg::OnAdd() 
{
	UpdateData(TRUE);

	if(!m_strNotesFieldName.GetLength())
		{
			AfxMessageBox("The Notes field name cannot be empty");
			return;
		}
	if(!m_strSharePointFieldName.GetLength())
		{
			AfxMessageBox("The SharePoint field name cannot be empty");
			return;
		}
	if(!m_strSharePointFieldType.GetLength())
		{
			AfxMessageBox("The SharePoint field type cannot be empty");
			return;
		}
	cNotesFieldInfo *pField=new cNotesFieldInfo();
	pField->m_strNotesName=m_strNotesFieldName;
	pField->m_strSharePointDisplayName=m_strSharePointFieldName;
	pField->m_strSharePointFieldType=m_strSharePointFieldType;
	m_objArrayCustomFields.Add(pField);
	n_Pos=m_objArrayCustomFields.GetUpperBound();
	m_slider.SetRangeMin(0);

	m_slider.SetRangeMax(n_Pos);

	
}

void CNotesFieldDlg::OnOpen() 
{
	UpdateData();
	CFile theFile;
	TRY
		{
			if(theFile.Open(m_strFileName, CFile::modeRead))
				{
				CArchive archive(&theFile, CArchive::load);


					m_objArrayCustomFields.Serialize(archive);
					archive.Flush();
					archive.Close();
					theFile.Close();
					cNotesFieldInfo *pField=(cNotesFieldInfo *)	m_objArrayCustomFields.GetAt(0);
					if(pField)
						{
							m_strNotesFieldName=pField->m_strNotesName;
							m_strSharePointFieldName=pField->m_strSharePointDisplayName;
							m_strSharePointFieldType=pField->m_strSharePointFieldType;
							UpdateData(FALSE);
						}

					m_slider.SetRangeMax(m_objArrayCustomFields.GetUpperBound());
				}
			else
				{
					CString cInfo;
					cInfo.Format("Could not open the file %s",m_strFileName);
					AfxMessageBox(cInfo);
					return;
				}

		}
	CATCH( CFileException, e )
		{
			if( e->m_cause == CFileException::fileNotFound )
        MessageBox( "ERROR: File not found\n");
			else
				e->ReportError();
		}
	END_CATCH
	
}

void CNotesFieldDlg::OnReleasedcaptureSlider1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	int nPos=m_slider.GetPos();
	if(nPos>=0 && nPos<=m_objArrayCustomFields.GetUpperBound())
		{
			n_Pos=nPos;
			cNotesFieldInfo *pField=(cNotesFieldInfo *)	m_objArrayCustomFields.GetAt(nPos);
			m_strNotesFieldName=pField->m_strNotesName;
			m_strSharePointFieldName=pField->m_strSharePointDisplayName;
			m_strSharePointFieldType=pField->m_strSharePointFieldType;
			UpdateData(FALSE);
		}
	
	
	*pResult = 0;
}

void CNotesFieldDlg::OnRemove() 
{
	if(n_Pos>=0 && n_Pos<=m_objArrayCustomFields.GetUpperBound())
		{
			cNotesFieldInfo *pField=(cNotesFieldInfo *)	m_objArrayCustomFields.GetAt(n_Pos);
			delete pField;
			m_objArrayCustomFields.RemoveAt(n_Pos);
			if(m_objArrayCustomFields.GetUpperBound()>0)
				{
					pField=(cNotesFieldInfo *)	m_objArrayCustomFields.GetAt(0);
					m_strNotesFieldName=pField->m_strNotesName;
					m_strSharePointFieldName=pField->m_strSharePointDisplayName;
					m_strSharePointFieldType=pField->m_strSharePointFieldType;
					UpdateData(FALSE);
				}
		}
	
	
}

void CNotesFieldDlg::OnBnClickedUpdate()
	{
	UpdateData(TRUE);

	if(!m_strNotesFieldName.GetLength())
		{
			AfxMessageBox("The Notes field name cannot be empty");
			return;
		}
	if(!m_strSharePointFieldName.GetLength())
		{
			AfxMessageBox("The SharePoint field name cannot be empty");
			return;
		}
	if(!m_strSharePointFieldType.GetLength())
		{
			AfxMessageBox("The SharePoint field type cannot be empty");
			return;
		}
	if(n_Pos>=0 && n_Pos<=m_objArrayCustomFields.GetUpperBound())
		{
			cNotesFieldInfo *pField=(cNotesFieldInfo *)	m_objArrayCustomFields.GetAt(n_Pos);
			if(pField)
				{
					pField->m_strNotesName=m_strNotesFieldName;
					pField->m_strSharePointDisplayName=m_strSharePointFieldName;
					pField->m_strSharePointFieldType=m_strSharePointFieldType;
				}
		}
	}
